<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>正则表达式-python | 后端学习记录</title><meta name="keywords" content="正则表达式"><meta name="author" content="h0ss"><meta name="copyright" content="h0ss"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="正则表达式1、正则模块python中有一个内置模块re，无需使用pip安装，直接 import re 2、主要方法1）findall(表达式, 处理字符串, 模式)特点： 会匹配出所有符合的项 模式： ①re.I 忽略大小写匹配 1234str1 &#x3D; &amp;#x27;python11C#\n11java66php&amp;#x27;a &#x3D; re.findall(&amp;#x27;c#&amp;#x27;, str1, re">
<meta property="og:type" content="article">
<meta property="og:title" content="正则表达式-python">
<meta property="og:url" content="https://blog.gpnusz.cn/2021/06/04/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F-python/index.html">
<meta property="og:site_name" content="后端学习记录">
<meta property="og:description" content="正则表达式1、正则模块python中有一个内置模块re，无需使用pip安装，直接 import re 2、主要方法1）findall(表达式, 处理字符串, 模式)特点： 会匹配出所有符合的项 模式： ①re.I 忽略大小写匹配 1234str1 &#x3D; &amp;#x27;python11C#\n11java66php&amp;#x27;a &#x3D; re.findall(&amp;#x27;c#&amp;#x27;, str1, re">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/09/01/4oSNuD31CVRgPQz.jpg">
<meta property="article:published_time" content="2021-06-03T16:25:32.000Z">
<meta property="article:modified_time" content="2022-07-28T07:11:30.574Z">
<meta property="article:author" content="h0ss">
<meta property="article:tag" content="正则表达式">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/09/01/4oSNuD31CVRgPQz.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://blog.gpnusz.cn/2021/06/04/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F-python/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-07-28 15:11:30'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">51</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">14</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">后端学习记录</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">正则表达式-python</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-06-03T16:25:32.000Z" title="发表于 2021-06-04 00:25:32">2021-06-04</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-07-28T07:11:30.574Z" title="更新于 2022-07-28 15:11:30">2022-07-28</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">939</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>4分钟</span></span></div></div></div><article class="post-content" id="article-container"><h2 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h2><h3 id="1、正则模块"><a href="#1、正则模块" class="headerlink" title="1、正则模块"></a>1、正则模块</h3><p>python中有一个内置模块re，无需使用pip安装，直接 import re</p>
<h3 id="2、主要方法"><a href="#2、主要方法" class="headerlink" title="2、主要方法"></a>2、主要方法</h3><h4 id="1）findall-表达式-处理字符串-模式"><a href="#1）findall-表达式-处理字符串-模式" class="headerlink" title="1）findall(表达式, 处理字符串, 模式)"></a>1）findall(表达式, 处理字符串, 模式)</h4><p>特点：</p>
<p>会匹配出所有符合的项</p>
<p>模式：</p>
<p>①re.I 忽略大小写匹配</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;python11C#\n11java66php&#x27;</span></span><br><span class="line">a = re.findall(<span class="string">&#x27;c#&#x27;</span>, str1, re.I) <span class="comment"># re.I 忽略大小写匹配</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[<span class="string">&#x27;C#&#x27;</span>]</span><br></pre></td></tr></table></figure>

<p>②re.S如果用.匹配可匹配所有字符包括换行符</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;python11C#\n11java66php&#x27;</span></span><br><span class="line"> <span class="comment"># 既忽略大小写又允许匹配换行符</span></span><br><span class="line">b = re.findall(<span class="string">&#x27;c#.&#123;1&#125;&#x27;</span>, str1, re.I | re.S)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[<span class="string">&#x27;C#\n&#x27;</span>]</span><br></pre></td></tr></table></figure>

<h4 id="2）re-sub-匹配表达式，替换的内容，操作的字符串，次数"><a href="#2）re-sub-匹配表达式，替换的内容，操作的字符串，次数" class="headerlink" title="2）re.sub(匹配表达式，替换的内容，操作的字符串，次数)"></a>2）re.sub(匹配表达式，替换的内容，操作的字符串，次数)</h4><p>特点：</p>
<p>1）替换的内容可以是一个函数也就是可以通过函数来处理匹配到的字符串</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;Python1C#2CPPJava3C++4C#5C#&#x27;</span></span><br><span class="line">a = re.sub(<span class="string">&#x27;C#&#x27;</span>, <span class="string">&#x27;CPP&#x27;</span>, str1, <span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">&quot;Python1CPP2CPPJava3C++4C#5C#&quot;</span></span><br></pre></td></tr></table></figure>

<p>2）如果替换内容是函数 ，传入函数的参数必须用：参数.group()取出值才能用 </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;Python1C#2Java3C++4C#5C#&#x27;</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">convert</span>(<span class="params">vaule</span>):</span></span><br><span class="line">    <span class="comment"># 传入的vaule为一个对象</span></span><br><span class="line">    <span class="comment"># 需要使用vaule.group() 取出匹配到的值</span></span><br><span class="line">    matches = vaule.group() </span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;!!&#x27;</span> + matches + <span class="string">&#x27;!!&#x27;</span></span><br><span class="line"></span><br><span class="line">a = re.sub(<span class="string">&#x27;C#&#x27;</span>, convert, str1, <span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">&quot;Python1!!C#!!2Java3C++4!!C#!!5!!C#!!&quot;</span></span><br></pre></td></tr></table></figure>

<h4 id="3）re-search-表达式-处理字符串-re-match-表达式-处理字符串"><a href="#3）re-search-表达式-处理字符串-re-match-表达式-处理字符串" class="headerlink" title="3）re.search(表达式, 处理字符串)  || re.match(表达式, 处理字符串)"></a>3）re.search(表达式, 处理字符串)  || re.match(表达式, 处理字符串)</h4><p>特点：</p>
<p>①只会匹配一次</p>
<p>②返回值是对象，并不是具体值</p>
<p>使用该方法返回的对象如何取出其中的值？</p>
<p>①用.group（0）取出匹配到的整个字符串</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;life is short,i use python&#x27;</span></span><br><span class="line"><span class="comment"># a1.group(0)返回匹配到的全部字符串</span></span><br><span class="line">a1 = re.search(<span class="string">&#x27;life(.*)python&#x27;</span>, str1)</span><br><span class="line"><span class="built_in">print</span>(a1.group(<span class="number">0</span>)) </span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">&quot;life is short,i use python&quot;</span></span><br></pre></td></tr></table></figure>

<p>②用.group（1）取出匹配的第一个元组数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;life is short,i use python&#x27;</span></span><br><span class="line">a1 = re.search(<span class="string">&#x27;life(.*)python&#x27;</span>, str1)</span><br><span class="line"><span class="comment"># a1.group(1)返回匹配的第一个元组数据内容 也就是(.*)</span></span><br><span class="line"><span class="built_in">print</span>(a1.group(<span class="number">1</span>))  </span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">&quot; is short,i use&quot;</span></span><br></pre></td></tr></table></figure>

<p>③用.groups()返回匹配的所有元组数据内容 不包括group(0)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;life is short,i use python&#x27;</span></span><br><span class="line">a1 = re.search(<span class="string">&#x27;life(.*)python&#x27;</span>, str1)</span><br><span class="line"><span class="comment"># a1.groups()返回匹配的所有元组数据内容 不包括group(0)</span></span><br><span class="line"><span class="built_in">print</span>(a1.groups())  </span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>(<span class="string">&#x27; is short,i use &#x27;</span>,)</span><br></pre></td></tr></table></figure>

<p>④用返回值.span()可以返回匹配的字符串所在位置</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;life is short,i use python&#x27;</span></span><br><span class="line">a1 = re.search(<span class="string">&#x27;life(.*)python&#x27;</span>, str1)</span><br><span class="line"><span class="built_in">print</span>(a1.span())</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>(<span class="number">0</span>, <span class="number">26</span>)</span><br></pre></td></tr></table></figure>

<h3 id="3、表达式及示例"><a href="#3、表达式及示例" class="headerlink" title="3、表达式及示例"></a>3、表达式及示例</h3><h4 id="1）-其中的每个字符为“或“的关系"><a href="#1）-其中的每个字符为“或“的关系" class="headerlink" title="1）[ ] 其中的每个字符为“或“的关系"></a>1）[ ] 其中的每个字符为“或“的关系</h4><h4 id="2）-x-表示匹配x个；-3-6-表示3-6个"><a href="#2）-x-表示匹配x个；-3-6-表示3-6个" class="headerlink" title="2）{x} 表示匹配x个；{3,6}表示3-6个"></a>2）{x} 表示匹配x个；{3,6}表示3-6个</h4><p>需要注意的是，python默认正则匹配为贪婪模式，也就是{x,y}会优先匹配y个。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;python 111java666php&#x27;</span></span><br><span class="line">a = re.findall(<span class="string">&#x27;[a-z]&#123;3,6&#125;&#x27;</span>, str1)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[<span class="string">&#x27;python&#x27;</span>, <span class="string">&#x27;java&#x27;</span>, <span class="string">&#x27;php&#x27;</span>]</span><br></pre></td></tr></table></figure>

<p>相对地，非贪婪模式表示为{x,y}？，这时只要匹配到x个就会返回结果。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> re</span><br><span class="line">str1 = <span class="string">&#x27;python 111java666php&#x27;</span></span><br><span class="line">aa = re.findall(<span class="string">&#x27;[a-z]&#123;3,6&#125;?&#x27;</span>, str1)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[<span class="string">&#x27;pyt&#x27;</span>, <span class="string">&#x27;hon&#x27;</span>, <span class="string">&#x27;jav&#x27;</span>, <span class="string">&#x27;php&#x27;</span>]</span><br></pre></td></tr></table></figure>

<h4 id="3）-匹配0次或者无限多次"><a href="#3）-匹配0次或者无限多次" class="headerlink" title="3）* 匹配0次或者无限多次"></a>3）* 匹配0次或者无限多次</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;pytho11python22pythonn33&#x27;</span></span><br><span class="line">a1 = re.findall(<span class="string">&#x27;python*&#x27;</span>, str1) </span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[<span class="string">&#x27;pytho&#x27;</span>, <span class="string">&#x27;python&#x27;</span>, <span class="string">&#x27;pythonn&#x27;</span>]</span><br></pre></td></tr></table></figure>

<h4 id="4）-匹配1次或者无限多次"><a href="#4）-匹配1次或者无限多次" class="headerlink" title="4）+ 匹配1次或者无限多次"></a>4）+ 匹配1次或者无限多次</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;pytho11python22pythonn33&#x27;</span></span><br><span class="line">a2 = re.findall(<span class="string">&#x27;python+&#x27;</span>, str1)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span> [<span class="string">&#x27;python&#x27;</span>, <span class="string">&#x27;pythonn&#x27;</span>]</span><br></pre></td></tr></table></figure>

<h4 id="5）-匹配0次或者1次"><a href="#5）-匹配0次或者1次" class="headerlink" title="5）? 匹配0次或者1次"></a>5）? 匹配0次或者1次</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str1 = <span class="string">&#x27;pytho11python22pythonn33&#x27;</span></span><br><span class="line">a3 = re.findall(<span class="string">&#x27;python?&#x27;</span>, str1)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[<span class="string">&#x27;pytho&#x27;</span>, <span class="string">&#x27;python&#x27;</span>, <span class="string">&#x27;python&#x27;</span>]</span><br></pre></td></tr></table></figure>

<h4 id="6）-表示从字符串开头匹配-，-表示从字符串末尾匹配"><a href="#6）-表示从字符串开头匹配-，-表示从字符串末尾匹配" class="headerlink" title="6）^表示从字符串开头匹配 ，$表示从字符串末尾匹配"></a>6）^表示从字符串开头匹配 ，$表示从字符串末尾匹配</h4><p>这种表达常用在用户名/手机号合法性校验中，以确保字符串为完整匹配。</p>
<p>例如：手机号校验 ^1[3-9][0-9]{9}$</p>
<p><img src="https://ftp.bmp.ovh/imgs/2021/06/4e4fd90836aacf91.jpg"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">h0ss</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://blog.gpnusz.cn/2021/06/04/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F-python/">https://blog.gpnusz.cn/2021/06/04/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F-python/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://blog.gpnusz.cn" target="_blank">后端学习记录</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">正则表达式</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/06/14/LeetCode%E6%AF%8F%E6%97%A5%E4%B8%80%E9%A2%9806-13/"><img class="prev-cover" src="https://img.yzcdn.cn/upload_files/2019/10/24/Fjj9BHxcDHPiux_bYihZe5YW4mLH.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">LeetCode每日一题06-13</div></div></a></div><div class="next-post pull-right"><a href="/2021/05/07/MySQL%E7%9F%A5%E8%AF%86%E6%95%B4%E5%90%88/"><img class="next-cover" src="https://tse3-mm.cn.bing.net/th/id/OIP.47fIp5Y4IN4qyJfkBELcqwHaHq?pid=ImgDet&amp;rs=1" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">MySQL入门知识整合</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">h0ss</div><div class="author-info__description">一个苦逼后端仔的学习记录</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">51</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">14</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/h0ss"><i class="fab fa-github"></i><span>Gitee仓库</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">Java后端技术栈语雀文档：https://www.yuque.com/oliver-wdtb1/java</div></div><div class="sticky_layout"><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2022/11/16/%E7%BB%99%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE%E6%8F%90%E4%BA%86%E4%B8%AAPR/" title="给开源项目提了个PR">给开源项目提了个PR</a><time datetime="2022-11-15T17:28:40.000Z" title="发表于 2022-11-16 01:28:40">2022-11-16</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2022/09/03/%E3%80%90MySQL%E3%80%91InnoDB%E7%9A%84%E8%A1%8C%E6%A0%BC%E5%BC%8F/" title="【MySQL】InnoDB的行格式">【MySQL】InnoDB的行格式</a><time datetime="2022-09-02T18:17:38.000Z" title="发表于 2022-09-03 02:17:38">2022-09-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2022/08/21/%E3%80%90Debug%E3%80%91bitField-%E5%BC%95%E5%8F%91%E7%9A%84%E6%A0%88%E6%BA%A2%E5%87%BA%E6%8E%92%E9%94%99%E8%AE%B0/" title="【Debug】bitField 引发的栈溢出排错记">【Debug】bitField 引发的栈溢出排错记</a><time datetime="2022-08-20T18:10:40.000Z" title="发表于 2022-08-21 02:10:40">2022-08-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2022/07/16/%E3%80%90RPC%E3%80%91%E8%AF%A6%E8%A7%A3SPI%E6%9C%BA%E5%88%B6/" title="【RPC】详解SPI机制">【RPC】详解SPI机制</a><time datetime="2022-07-15T18:51:57.000Z" title="发表于 2022-07-16 02:51:57">2022-07-16</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2022/07/14/%E3%80%90Redis%E3%80%91%E9%87%8D%E8%A6%81%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%9F%BA%E7%A1%80/" title="【Redis】重要数据结构基础">【Redis】重要数据结构基础</a><time datetime="2022-07-13T17:22:08.000Z" title="发表于 2022-07-14 01:22:08">2022-07-14</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="爱国,敬业,诚信,友善,自由,平等,公正,法治,富强,民主,文明,和谐" data-fontsize="16px" data-random="false" async="async"></script></div></body></html>